'use strict';

const { Controller } = require('egg');
const path = require('path');
const { v4: v4uuid } = require('uuid');

class OssController extends Controller {
  async upload() {
    const { ctx, app } = this;
    const file = ctx.request.files[0];
    const pathName = '/all-poster-imgs/' + v4uuid() + path.extname(file.filename);
    const data = await ctx.oss.put(pathName, file.filepath);
    ctx.helper.success({ ctx, res: data });
  }
  // 删除 测试接口
  async delete() {
    const data = await this.ctx.oss.delete(
      'all-poster-imgs/36.041536484098.jpeg'
    );
    this.ctx.body = data;
  }
  // 下载 测试接口
  async download() {
    const { filePath } = this.ctx.request.body;
    const data = await this.ctx.oss.get(
      filePath,
      path.join(
        __dirname,
        `../public/imgs/${Math.random() * 100}_${path.extname(filePath)}`
      )
    );
    this.ctx.body = data;
  }
}

module.exports = OssController;
